package com.work.formaldehyde.application;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;

import com.lljjcoder.style.citylist.utils.CityListLoader;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class App extends Application {


    public void onCreate() {
        super.onCreate();
        //初始化获取网络状态
        ApiUtils.isNetworkConnecteds = ApiUtils.isNetworkConnected(this);
        CityListLoader.getInstance().loadCityData(this);
        createDB(this, ApiUtils.DB_NAME);
        initWX();
        getuser();
        UMConfigure.init(this,Url.USDK
                ,"xiaomi",UMConfigure.DEVICE_TYPE_PHONE,"");
        PlatformConfig.setWeixin(Url.WXAPPID, "86c86df59cedffe03daf6090a28badd6");
//        UMConfigure.setLogEnabled(true);
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_MANUAL);
        UMConfigure.setProcessEvent(true);
    }


    /**
     * 注册微信
     */
    public void initWX() {
        final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
        msgApi.registerApp(Url.WXAPPID);
    }

    public void createDB(Context context, String dbname) {
        File file = new File(getFilesDir(), dbname);
        if (!file.exists()) {
            AssetManager assets = getAssets();
            FileOutputStream fos = null;
            InputStream is = null;
            try {
                is = assets.open(dbname);
                fos = new FileOutputStream(file);
                byte[] b = new byte[1024];
                int len = -1;
                while ((len = is.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        ApiUtils.DB_LOCTION = context.getDatabasePath(dbname).getPath();
    }

    private SharedPreferences mSharedPreferences;
    private SharedPreferences.Editor mEditor;
    private SharedPreferences mSharedPreferences1;
    private SharedPreferences.Editor mEditor1;

    public void getuser() {
        mSharedPreferences = getSharedPreferences("user", MODE_PRIVATE);  //名称和类型
        mEditor = mSharedPreferences.edit();
        Url.USER_NAME = mSharedPreferences.getString("user_name", "");
        Url.USER_ID = mSharedPreferences.getString("user_id", "");
        Url.USER_WECHAT_NAME = mSharedPreferences.getString("user_wechat", "");
        Url.USER_ALIPAY_NAME = mSharedPreferences.getString("user_zfb_zh", "");
        Url.USER_PHONE_NAME = mSharedPreferences.getString("user_phone", "");
        Url.USER_ALIPAY_USER_NAME = mSharedPreferences.getString("user_zfb_name", "");
        Url.USER_HEAD_IMG = mSharedPreferences.getString("user_head", "");
        Url.USER_NICK_NAME = mSharedPreferences.getString("user_nickname", "");
        Url.LOGIN_TOKEN = mSharedPreferences.getString("logintoken", "");
        mSharedPreferences1 = getSharedPreferences("login", MODE_PRIVATE);  //名称和类型
        mEditor1 = mSharedPreferences1.edit();
        Url.QIDONGNUM = mSharedPreferences1.getString("islogin", "");
    }

}
